package liming.ai.core.impl.deepseek;

import liming.tool.file.FileRW;
import org.json.JSONObject;

public class DeepSeekConfig {
    private String apiKey = "";
    private String encoding_read = "";
    private String encoding_write = "";
    private String title = "";
    private long connectTimeout = 10000;
    private long readTimeout = 30000;
    private long writeTimeout = 15000;
    private int maxRetries = 3;
    private long initialTimeOut = 3000;
    private FileRW.FileEncoding fileWriterEncoding;
    private FileRW.FileEncoding systemReaderEncoding;

    public boolean initConfig(JSONObject config) {
        boolean changed = false;
        if (config.has("deepseek")) {
            JSONObject deepseek = config.getJSONObject("deepseek");
            if (deepseek.has("apiKey"))
                apiKey = deepseek.optString("apiKey", "");
            if (deepseek.has("encoding-r"))
                encoding_read = deepseek.optString("encoding-r", "");
            if (deepseek.has("encoding-w"))
                encoding_write = deepseek.optString("encoding-w", "");
            if (deepseek.has("connectTimeout"))
                connectTimeout = deepseek.optLong("connectTimeout", 10_000);
            if (deepseek.has("readTimeout"))
                readTimeout = deepseek.optLong("readTimeout", 30_000);
            if (deepseek.has("writeTimeout"))
                writeTimeout = deepseek.optLong("writeTimeout", 15_000);
            if (deepseek.has("title"))
                title = deepseek.optString("title", "");
            if (deepseek.has("maxRetries"))
                maxRetries = deepseek.optInt("maxRetries", 3);
            if (deepseek.has("initialTimeOut"))
                initialTimeOut = deepseek.optLong("initialTimeOut", 3_000);
        }else {
            changed = true;
        }
        config.put("deepseek", toJSON());
        fileWriterEncoding = FileRW.FileEncoding.fromName(encoding_write);
        systemReaderEncoding = FileRW.FileEncoding.fromName(encoding_read);
        return changed;
    }

    public String getApiKey() {
        return apiKey;
    }

    public FileRW.FileEncoding getFileWriterEncoding() {
        return fileWriterEncoding;
    }

    public FileRW.FileEncoding getSystemReaderEncoding() {
        return systemReaderEncoding;
    }

    public long getConnectTimeout() {
        return connectTimeout;
    }

    public long getReadTimeout() {
        return readTimeout;
    }

    public long getWriteTimeout() {
        return writeTimeout;
    }

    public String getTitle() {
        return title;
    }

    public int getMaxRetries() {
        return maxRetries;
    }

    public long getInitialTimeOut() {
        return initialTimeOut;
    }

    public JSONObject toJSON() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("apiKey", apiKey);
        jsonObject.put("encoding-r", encoding_read);
        jsonObject.put("encoding-w", encoding_write);
        jsonObject.put("connectTimeout", connectTimeout);
        jsonObject.put("readTimeout", readTimeout);
        jsonObject.put("writeTimeout", writeTimeout);
        jsonObject.put("title", title);
        jsonObject.put("maxRetries", maxRetries);
        jsonObject.put("initialTimeOut", initialTimeOut);
        return jsonObject;
    }
}
